import { useState, useCallback } from 'react';
import { useBlogApi } from '../services/api';
import { useBaseModel } from '../hooks/baseModel';
import { IList, IComment } from '@/interface';
import useLoading from '@/hooks/loding';
import { useSnackbar } from 'notistack';


export default function Blog(){
  // const params: any = useParams()
  const blogApi = useBlogApi()

  // const loadList = useCallback(, [])
  const listModel = useBaseModel(() => blogApi.list(0))
  listModel.loadData.autoRun([])

  // console.log(loadList);
  

  // const loadInfo = useCallback(, [])
  const infoModel = useBaseModel((id: string) => blogApi.info(id))

  const commentModel = useBaseModel((id: string) => blogApi.commentList(id))

  const { enqueueSnackbar } = useSnackbar();
  const submitComment = useLoading(async (id: string, data: any, reset?: () => void) => {
    const comment = await blogApi.addComment(id, data)
    const commentData = commentModel.data || {} as IList<IComment>
    const commentList = commentData.list || []
    commentModel.setData({
      ...commentData,
      list: [
        comment,
        ...commentList
      ]
    })
    enqueueSnackbar("发布成功", {
      variant: 'success'
    })
    reset && reset()
  }, [commentModel.data, commentModel.data?.count || 0])

  return {
    listModel,
    infoModel,
    commentModel,
    submitComment
  }
}